package com.uroad.jilinpda.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.uroad.jilinpda.activity.LoginActivity;
import com.uroad.jilinpda.layer.LoginLayer;

public class LoginPresenter extends BaseMvpPresenter<LoginActivity>{

    private LoginLayer layer;
    LoginActivity view;

    public LoginPresenter(Activity activity){
        this.layer = new LoginLayer(activity, new LoginCallBack());
    }

    public void checkAttach(){
        view = checkViewAttach();
    }

    public void sendCode(String phone){
        String resutl = layer.sendCode(phone);
        if (!TextUtils.isEmpty(resutl)){
            view.showToast(resutl);
        }
    }

    public void login(String phone, String pwd){
        String resutl = layer.login(phone, pwd);
        if (!TextUtils.isEmpty(resutl)){
            view.showToast(resutl);
        }
    }

    private class LoginCallBack implements LoginLayer.LoginLayerCallBack {

        @Override
        public void timeControllerCallBack(String resutl, long time) {
            view.setTvSendcode(resutl);
            if (time == -1)
                view.tvSendcodeClickAble(true);
            else
                view.tvSendcodeClickAble(false);
        }

        @Override
        public void onSuccess() {
            view.openMainActivty();
        }

        @Override
        public void onFailure(String fail) {
            view.showToast(fail);
        }
    }

}
